package com.curluin.controll;

import com.curluin.model.ModelObject;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class JukeBox {



	/**
	 * Sounds
	 */
	private MediaPlayer soundFallInWater,soundSlide, soundIceCrash, soundBackground;
	private int defaultVolumeFallInWater, defaultVolumeSlide,defaultVolumeIceCrash, defaultVolumeBackground=100;
	private static final String LOG_TAG = "JukeBox";
	private static final double MAX_VOLUME =100;
	private boolean mute;

	public JukeBox(Context context){
		this.initializeSounds(context);
	}


	private void initializeSounds(Context context){
		if(soundBackground==null){
			soundBackground = MediaPlayer.create(context, R.raw.adorehomelesschildren); 
			soundBackground.setLooping(true);
			defaultVolumeBackground = 80;
			setVolume(soundBackground, defaultVolumeBackground);
		}
		if(soundSlide==null){
			soundSlide = MediaPlayer.create(context, R.raw.rockfacewaterfall);
			defaultVolumeSlide = 3;
			setVolume(soundSlide, defaultVolumeSlide);
			soundSlide.setLooping(true);
		}
		if(soundIceCrash==null) soundIceCrash = MediaPlayer.create(context,R.raw.iceimpactheavy);
		
		if(soundFallInWater==null)  soundFallInWater = MediaPlayer.create(context, R.raw.watersplash);
	}

	/**
	 * Plays Background sound(in loop)
	 */
	public void playBackGroundSound(){
		//adorehomelesschildren not licensed from: http://www.jamendo.com/de/search#qs=q=Homeless%20Children
		if(soundBackground!=null && !soundBackground.isPlaying()) soundBackground.start();
	}

	/**
	 * Pause background sound(in loop)
	 */
	public void pauseBackGroundSound(){
		if(soundBackground!=null && soundBackground.isPlaying()) soundBackground.pause();
	}

	/**
	 * Plays sound if something falls into water.
	 */
	private void playFallIntoWaterSound(){
		//bubbleblownintoglass or watersplash from:http://www.freesfx.co.uk/soundeffects/nature/
		if(soundFallInWater!=null && !soundFallInWater.isPlaying()) soundFallInWater.start();
	}

	/**
	 * Plays sliding sound(in loop)
	 */
	public void playSlidingSound(){
		//rockfacewaterfall from:http://www.freesfx.co.uk/soundeffects/nature/
		if(soundSlide!=null && !soundSlide.isPlaying()) soundSlide.start();
	}

	/**
	 * Pauses sound of sliding
	 */
	public void pauseSlidingSound(){
		if(soundSlide!=null && soundSlide.isPlaying()) soundSlide.pause();
	}

	/**
	 * Collision with ice
	 */
	private void playCollisionIceSound(){
		//iceimpactheavy from http://www.freesfx.co.uk/soundeffects
		if(soundIceCrash!=null && !soundIceCrash.isPlaying()) soundIceCrash.start();
	}

	/**
	 * Plays collision sound dependent of the two consitencies
	 * @param firstConsitency
	 * @param secondConsitency
	 */
	public void playCollisionSound(int firstConsitency, int secondConsitency){
		Log.w(LOG_TAG, "Play Collision Sound");
		if(firstConsitency==ModelObject.CONSITENCY_FLUID||secondConsitency==ModelObject.CONSITENCY_FLUID)
			playFallIntoWaterSound();
		else if(firstConsitency==ModelObject.CONSITENCY_HARD_AS_ICE||secondConsitency==ModelObject.CONSITENCY_HARD_AS_ICE)
			playCollisionIceSound();

	}

	public void releaseAllSounds(){
		if(soundFallInWater!=null)soundFallInWater.release();
		if(soundSlide!=null)soundSlide.release();
		if(soundIceCrash!=null)soundIceCrash.release();
		if(soundBackground!=null)soundBackground.release();
	}

	public void muteAllSounds(){
		setVolume(soundFallInWater, 0);
		setVolume(soundSlide, 0);
		setVolume(soundIceCrash,0);
		setVolume(soundBackground,0);
		mute =true;
	}
	public void resetSoundVolumes(){
		setVolume(soundFallInWater, defaultVolumeFallInWater);
		setVolume(soundSlide ,defaultVolumeSlide);
		setVolume(soundSlide , defaultVolumeIceCrash);
		setVolume(soundBackground, defaultVolumeBackground);
		mute=false;
	}

	public boolean isMute(){
		return mute;
	}

	private void setVolume(MediaPlayer mplayer, int vol){
		if(mplayer!=null){
			final float volume = (float) (1 - (Math.log(MAX_VOLUME - vol) / Math.log(MAX_VOLUME)));
			mplayer.setVolume(volume, volume);
			Log.w(LOG_TAG,"new Volume: "+volume);
		}
	}
}
